// 权限管理
<template>
  <div class="permission-container">
    <PageTools>
      <el-button slot="after" type="primary" size="small" @click="addPermission(1, '0')">添加权限</el-button>
    </PageTools>
    <el-table row-key="id" :data="list" border stripe>
      <el-table-column prop="name" align="left" label="菜单名称" />
      <el-table-column prop="code" align="center" label="标识" />
      <el-table-column prop="description" align="center" label="描述" />
      <el-table-column align="center" label="操作">
        <template v-slot="{ row }">
          <el-button v-if="row.type === 1" size="mini" type="primary" @click="addPermission(2, row.id)">添加</el-button>
          <el-button size="mini" type="warning" @click="editPermission(row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="delPermission(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增和编辑弹出层 -->
    <el-dialog :title="showText" :visible.sync="showDialog" @close="btnCancel">
      <el-form ref="formData" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width：90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width：90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width：90%" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch v-model="formData.isVisible" active-value="1" inactive-value="0" style="width：90%" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center" align="middle">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import {
  getPermissionList,
  deletePermission,
  addPermission,
  updatePermission,
  getPermissionDetail
} from '@/api/permission'
import { tranListToTreeDate } from '@/utils'

export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: '0'
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '权限名称不能为空' }],
        code: [{ required: true, trigger: 'blur', message: '权限标识不能为空' }]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const result = await getPermissionList()
      this.list = tranListToTreeDate(result, '0')
    },
    //  删除操作
    delPermission(id) {
      this.$confirm('确定要删除该权限么？')
        .then(() => {
          return deletePermission(id)
        })
        .then(() => {
          this.$message.success('删除成功！')
          this.getPermissionList()
        })
    },
    // 添加 type 1: 访问权  2: 按钮操作权
    addPermission(type, id) {
      this.formData.type = type
      this.formData.pid = id
      this.showDialog = true
    },
    // 编辑
    async editPermission(id) {
      this.showDialog = true
      this.formData = await getPermissionDetail(id)
    },
    btnCancel() {
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: '',
        pid: '',
        enVisible: '0'
      }
      this.$refs.formData.resetFields()
      this.showDialog = false
    },
    btnOk() {
      this.$refs.formData
        .validate()
        .then(() => {
          if (this.formData.id) {
            return updatePermission(this.formData)
          }
          return addPermission(this.formData)
        })
        .then(() => {
          this.$message.success('操作成功')
          this.getPermissionList()
          this.showDialog = false
        })
    }
  }
}
</script>

<style scoped lang="scss">
.permission-container {
  padding: 15px;
  ::v-deep .el-dialog__header {
    background-color: rgb(29, 63, 255);
    .el-dialog__title,
    .el-dialog__close {
      color: #fff;
    }
  }
}
</style>
